package com.aaa.docter.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.aaa.common.entity.User;
import com.aaa.common.vo.Result;
import com.aaa.docter.dao.SchedulingDao;
import com.aaa.docter.dto.SchedulingDTO;
import com.aaa.docter.feign.UserFeign;
import com.aaa.docter.service.SchedulingService;
import com.aaa.docter.vo.SchedulingVo;
import com.aaa.entity.Scheduling;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @program: aaa-hospital-parent
 * @description:
 * @author: 闫克起
 * @create: 2024-05-24 16:54
 **/
@Service
public class SchedulingServiceImpl implements SchedulingService {
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private SchedulingDao schedulingDao;
    @Override
    public Result<Map<String,Object>> docterScheduling(SchedulingVo schedulingVo) {
        //获取所有值班的医生信息
        Map<String,Object> map=new HashMap<>();
        map.put("deptId",schedulingVo.getDeptId());
        map.put("userId",schedulingVo.getUserId());
        Result<List<User>> scheduling = userFeign.getScheduling(map);
        List<User> data = scheduling.getData();
        if(data==null){
            throw new RuntimeException("用户服务器错误");
        }
        return findResult(schedulingVo,data);
    }

    private Result<Map<String,Object>> findResult(SchedulingVo schedulingVo, List<User> users) {
        Map<String,Object> map=new HashMap<>();
         //1.求是否传递查询日期
        String queryDate = schedulingVo.getQueryDate();
        //2. 创建一个当前日期
        DateTime now = DateUtil.date();
        //3. 判断查询
        if(StringUtils.hasText(queryDate)){
            DateTime parse = DateUtil.parse(queryDate, "yyyy-MM-dd");
            if(DateUtil.dayOfWeek(parse)==1){//表示周日
                now= DateUtil.offsetDay(parse,1);
            }else{
                now= DateUtil.offsetDay(parse,-1);
            }
        }
        //4. 获取这周的开始日期
        DateTime begin = DateUtil.beginOfWeek(now);
        DateTime end = DateUtil.endOfWeek(now);
        schedulingVo.setBeginDate(DateUtil.format(begin,"yyyy-MM-dd"));
        schedulingVo.setEndDate(DateUtil.format(end,"yyyy-MM-dd"));
        //5. 根据条件查询这周的值班记录
        List<Scheduling> schedulings= selectSchedulingByCon(schedulingVo);

        List<SchedulingDTO> list=new ArrayList<>();
        for(User user:users){
            SchedulingDTO schedulingDTO1 = new SchedulingDTO(user.getUserId(), user.getDeptId(), "1", initMap(begin));
            SchedulingDTO schedulingDTO2 = new SchedulingDTO(user.getUserId(), user.getDeptId(), "2", initMap(begin));
            SchedulingDTO schedulingDTO3 = new SchedulingDTO(user.getUserId(), user.getDeptId(), "3", initMap(begin));
            for(Scheduling scheduling:schedulings){
                String schedulingDay = scheduling.getSchedulingDay();
                String schedulingType = scheduling.getSchedulingType();
                String subsectionType = scheduling.getSubsectionType();
                Integer userId = scheduling.getUserId();
                if(user.getUserId().equals(userId)){
                     switch (subsectionType){
                         case "1":
                             Map<String, String> record = schedulingDTO1.getRecord();
                             record.put(schedulingDay,schedulingType);
                             break;
                         case "2":
                             Map<String, String> record2 = schedulingDTO1.getRecord();
                             record2.put(schedulingDay,schedulingType);
                             break;
                         case "3":
                             Map<String, String> record3 = schedulingDTO1.getRecord();
                             record3.put(schedulingDay,schedulingType);
                             break;
                     }
                }
            }
            schedulingDTO1.setSchedulingType(schedulingDTO1.getRecord().values());
            schedulingDTO2.setSchedulingType(schedulingDTO2.getRecord().values());
            schedulingDTO3.setSchedulingType(schedulingDTO3.getRecord().values());

            list.add(schedulingDTO1);
            list.add(schedulingDTO2);
            list.add(schedulingDTO3);
        }

        //获取头标题
        List<String> labelNames=labelName(begin);

        map.put("labelNames",labelNames);
        map.put("beginDate",DateUtil.format(begin,"yyyy-MM-dd"));
        map.put("endDate",DateUtil.format(end,"yyyy-MM-dd"));
        map.put("list",list);

        return new Result<Map<String,Object>>(2000,"查询",map);


    }

    private List<String> labelName(DateTime begin) {
        List<String> labelNames=new ArrayList<>();
        for(int i=0;i<7;i++){
            DateTime dateTime = DateUtil.offsetDay(begin, i);
            String name=DateUtil.format(dateTime,"yyyy-MM-dd")+week(i);
            labelNames.add(name);
        }
        return labelNames;
    }

    private String week(int i) {
            String week="";
            switch (i){
                case 0:
                    week="(周一)";
                    break;
                case 1:
                    week="(周二)";
                    break;
                case 2:
                    week="(周三)";
                    break;
                case 3:
                    week="(周四)";
                    break;
                case 4:
                    week="(周五)";
                    break;
                case 5:
                    week="(周六)";
                    break;
                case 6:
                    week="(周日)";
                    break;
            }
            return week;

    }

    private Map<String, String> initMap(DateTime begin) {
        Map<String,String> map=new HashMap<>();
        for(int i=0;i<7;i++){
            DateTime dateTime = DateUtil.offsetDay(begin, i);
            map.put(DateUtil.format(dateTime,"yyyy-MM-dd"),"");
        }
        return map;
    }

    private List<Scheduling> selectSchedulingByCon(SchedulingVo schedulingVo) {
        QueryWrapper<Scheduling> wrapper=new QueryWrapper<>();
        if(Objects.nonNull(schedulingVo.getUserId())){
            wrapper.eq("user_id",schedulingVo.getUserId());
        }
        if(Objects.nonNull(schedulingVo.getDeptId())){
            wrapper.eq("dept_id",schedulingVo.getDeptId());
        }
        if(StringUtils.hasText(schedulingVo.getBeginDate())){
            wrapper.ge("scheduling_day",schedulingVo.getBeginDate());
        }
        if(StringUtils.hasText(schedulingVo.getEndDate())){
            wrapper.le("scheduling_day",schedulingVo.getEndDate());
        }
        List<Scheduling> schedulings = schedulingDao.selectList(wrapper);
        return schedulings;
    }
}


















